package Coller

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"week2/iniitialize"
	"week2/models"
)

func Spike(c *gin.Context) {
	UserId, _ := strconv.Atoi(c.PostForm("user_id"))       //用户id
	ShopId, _ := strconv.Atoi(c.PostForm("shop_id"))       //商品id
	Gotime := c.PostForm("go_time")                        //开始时间
	Stoptime := c.PostForm("stop_time")                    //结束时间
	ShopNum, _ := strconv.Atoi(c.PostForm("shop_num"))     //商品数量
	ShopPrice, _ := strconv.Atoi(c.PostForm("shop_price")) //商品单价
	ShopMeny, _ := strconv.Atoi(c.PostForm("shop_meny"))   //商品总价格
	ShopCount, _ := strconv.Atoi(c.PostForm("shop_count")) //商品总数量
	pone := models.Spike{
		UserId:    UserId,
		ShopId:    ShopId,
		Gotime:    Gotime,
		Stoptime:  Stoptime,
		ShopNum:   ShopNum,
		ShopPrice: float64(ShopPrice),
		ShopMeny:  ShopMeny,
		ShopCount: ShopCount,
	}
	err := pone.CreateSpike()
	e, _ := json.Marshal(pone.ShopNum)
	iniitialize.Rdb.Get(string(e))
	tx := iniitialize.Rdb.Do()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"Code": -1,
			"Msg":  "添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "添加成功",
	})
	tx.Val()
	_, err = tx.Result()

}

func SpikeUpdate(c *gin.Context) {
	UserId, _ := strconv.Atoi(c.PostForm("user_id"))       //用户id
	ShopId, _ := strconv.Atoi(c.PostForm("shop_id"))       //商品id
	Gotime := c.PostForm("go_time")                        //开始时间
	Stoptime := c.PostForm("stop_time")                    //结束时间
	ShopNum, _ := strconv.Atoi(c.PostForm("shop_num"))     //商品数量
	ShopPrice, _ := strconv.Atoi(c.PostForm("shop_price")) //商品单价
	ShopMeny, _ := strconv.Atoi(c.PostForm("shop_meny"))   //商品总价格
	ShopCount, _ := strconv.Atoi(c.PostForm("shop_count")) //商品总数量
	pone := models.Spike{
		UserId:    UserId,
		ShopId:    ShopId,
		Gotime:    Gotime,
		Stoptime:  Stoptime,
		ShopNum:   ShopNum,
		ShopPrice: float64(ShopPrice),
		ShopMeny:  ShopMeny,
		ShopCount: ShopCount,
	}
	err := pone.CreateSpike()
	if err != nil {
		return
	}
	e, _ := json.Marshal(pone.ShopNum)
	iniitialize.Rdb.Get(string(e))
	c.JSON(http.StatusOK, gin.H{
		"Code": -1,
		"Msg":  "扣减成功",
	})
}
